package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.base.util.PasswordUtil;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
/*应用启动时的处理过程
Spring扫描到@Service注解的UserServiceImpl类
创建UserServiceImpl实例并注册到Spring容器中
当处理UserController时，发现@Autowired UserService userService注解
Spring在容器中查找UserService类型的Bean，找到UserServiceImpl实例
将UserServiceImpl实例注入到userService变量中*/
public class UserServiceImpl implements UserService {
    /*@Autowired UserMapper userMapper; 的作用是获取一个可以执行数据库操作的代理对象，而不是直接接收查询结果。*/
    @Autowired
    UserMapper userMapper;
    @Override//业务层实现类需要添加@Service注解,表示其为业务层实现类
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录功能业务参数:{}",userLoginParam);
        UserVO userVO=userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("查询结果:{}",userVO);
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list=userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("新增用户:userSaveParam={}",userSaveParam);
        User user=new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){//新增
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{//编辑
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码:userId={}",userId);
        User user=new User();
        user.setId(userId);
//        user.setPassword("root");
        user.setPassword(PasswordUtil.generatePassword(7));
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, Integer status) {
        log.debug("更新状态:userId={},status={}",userId,status);
        User user=new User();
        user.setId(userId);
        user.setStatus(status.toString());
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户:userId={}",userId);
        userMapper.delete(userId);
    }

    @Override
    public List<UserVO> selectAuditUserList(Long parentId) {
        log.debug("查询审批人:parentId={}",parentId);
        //1.准备一个空集合，用来保存查出来的多个审批人
        ArrayList<UserVO> userVOList=new ArrayList<>();
        //2.现根据id把直属领导查出来
        UserVO auditUser1=userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //3.如果有直属领导，且直属领导还有领导，将上上级查出来
        if (auditUser1!=null && auditUser1.getParentId()!=null){
            UserVO auditUser2=userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }

}
